package com.tjyx.common.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 所有Action的基类
 * 
 * @author zhutianjie
 * 
 */
public abstract class BaseAction {

	protected int page; //起始页
	protected int rows; //最大显示数
	protected String order; //排序方式
	protected String sort; //被排序的字段
	protected Map<String,Object> sqlMap = new HashMap<String,Object>(); //装载字段"属性-值"，用于构造SQL语句
	/**
	 * 出现错误跳转的页面
	 */
	protected static final String ERROR_PAGE = "error.jsp"; 
	/**
	 * 错误信息的request属性
	 */
	protected static final String ERROR_ATTR = "errMsg"; 
	
	/**
	 * 操作成功的OK提示
	 */
	protected static final String OK = "ok";
	/**
	 * 不进行转发，返回原始页面
	 */
	protected static String NO_DISPATCH = null; 
	

	/**
	 * 初始化分页参数 page,rows,order
	 * @param request
	 */
	public void initPagination(HttpServletRequest request) {
		setPage(request);
		setRows(request);
		setOrder(request);
	}

	/**
	 * 设置页码
	 * @param request
	 */
	public void setPage(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		this.page = page;
	}

	/**
	 * 设置页面显示数量
	 * @param request
	 */
	public void setRows(HttpServletRequest request) {
		int rows = Integer.parseInt(request.getParameter("rows"));
		this.rows = rows;
	}

	/**
	 * 设置排序方式
	 * @param request
	 */
	public void setOrder(HttpServletRequest request) {
		String order = request.getParameter("order");
		this.order = order;
	}
	
	/**
	 * 设置排序字段
	 * @param request
	 */
	public void setSort(HttpServletRequest request) {
		String sort = request.getParameter("sort");
		this.sort = sort;
	}
	
	/**
	 * 将信息写经输出流写到页面
	 * 
	 * @param message 输出的字符数据
	 * @param response HTTP响应
	 * @return
	 * @throws IOException
	 */
	public void write2View(Object message,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.write(message.toString()); //返回页面JSON信息
	}
}
